package xcmg.device.web.sluggishShard;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.sluggishShard.SluggishShardVO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.sluggishShard.SluggishShardService;
import xcmg.device.service.vo.FunctionResult;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

/**
 * @author ZHANGK
 * @since 2020/6/5
 **/
@RestController
@RequestMapping("/sluggishShard")
public class SluggishShardController extends BasicController {

    @Autowired
    SluggishShardService sluggishShardService;

    @PostMapping("/getInsertList")
    public ActionResult<PageInfo> getInsertList(@RequestBody SearchCommonVO<SluggishShardVO> condition) throws Exception {
        String orgId = getOrgId();
        condition.getFilters().setOrgId(orgId);
        return actionValueResult(sluggishShardService.getInsertList(condition).getPageInfo());
    }

    @PostMapping("/add")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody SluggishShardVO sluggishShardVO) {
        return actionResult(sluggishShardService.insert(sluggishShardVO));
    }

    @PostMapping("/importExcel")
    public ActionResult importExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartHttpServletRequest.getFile(multipartHttpServletRequest.getFileNames().next());
        String filename = file.getOriginalFilename();
        FunctionResult<String> result = new FunctionResult<>();
        String orgId = getOrgId();
        if (!filename.trim().endsWith(".xlsx")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else{
            InputStream in = file.getInputStream();
            result = sluggishShardService.importExcel(in,orgId);
        }
        return actionResult(result.getCode(),result.getValue());
    }

    @PostMapping("/update")
    public ActionResult update(@RequestBody SluggishShardVO sluggishShardVO) throws Exception {
        String orgId = getOrgId();
        if (!sluggishShardVO.getOrgId().equals(orgId)) {
            return actionResult(ErrorCode.SluggishEditError);
        }
        return actionResult(sluggishShardService.update(sluggishShardVO));
    }

    @PostMapping("/delete")
    public ActionResult delete(@RequestBody SluggishShardVO sluggishShardVO) throws Exception {
        String orgId = getOrgId();
        if (!sluggishShardVO.getOrgId().equals(orgId)) {
            return new ActionResult(ErrorCode.Failure.getCode(),"不能删除非本企业的数据");
        }
        return actionResult(sluggishShardService.deleteById(sluggishShardVO.getId()));
    }

    @PostMapping("/list")
    public ActionResult list(@RequestBody SearchCommonVO<SluggishShardVO> condition){
        PageCommonVO<SluggishShardVO> pageCommonVO = sluggishShardService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/downloadImportExcel",method = RequestMethod.POST)
    public void downloadImportExcel() throws Exception {
        sluggishShardService.downloadImportExcel();
    }

    @PostMapping("/export")
    public void export(String[] ids) throws Exception {
        sluggishShardService.export(ids);
    }

    @PostMapping("/exportAll")
    public void export(SluggishShardVO sluggishShardVO) throws Exception {
        sluggishShardService.exportAll(sluggishShardVO);
    }

}
